home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 719 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.8 KB  |  77 lines

  1. Path: canberra.DIALix.oz.au!not-for-mail
  2. From: fruitbat@canberra.DIALix.oz.au (Paul Sleigh)
  3. Newsgroups: comp.lang.c++
  4. Subject: Idle curiosity: Delphi properties in C++
  5. Date: 7 Jan 1996 01:38:49 +1100
  6. Organization: DIALix Services, Canberra, Australia.
  7. Sender: fruitbat@canberra.DIALix.oz.au
  8. Message-ID: <4cm1hp$mok$1@canberra.DIALix.oz.au>
  9. NNTP-Posting-Host: fruitbat@canberra.dialix.oz.au
  10.  
  11. I freely admit I'm a Delphi programmer, but I also like playing with any
  12. language I can find, and thinking about them (what can I say -- my
  13. girlfriend's away for the weekend).  So here's a puzzle for those more
  14. knowledgeable than I:
  15.  
  16. One major advantage Delphi has over Borland Pascal 7.0, Visual Basic and
  17. (apparently) C++ is called a property.  If you'll excuse a breath of Pascal
  18. in this holy of holies C++ group, I'll demonstrate the use of this feature:
  19.  
  20. type
  21.   TDate = record Year, Month, Day: Integer; end;
  22.   TPerson = class
  23.     private
  24.       FBirthday: TDate;
  25.       procedure SetBirthday(ABirthday: TDate);
  26.       function GetAge: Integer;
  27.     public
  28.       property Birthday: TDate  read FBirthday  write SetBirthday;
  29.       property Age: Integer  read GetAge;
  30.   end;
  31.  
  32. For the benefit of any of you who never had the good fortune to learn
  33. Pascal, I think the simplest C++ equivalent would be as listed at the end of
  34. this article.
  35.  
  36. Basically, how it works is that, given the following definitions:
  37.  
  38. var
  39.   P: TPerson;
  40.   A: Integer;
  41.   D: TDate;
  42.  
  43. you can do the following, with the actual results as listed in comments:
  44.  
  45. P.Birthday := D;                       (* exactly like P.SetBirthday(D); *)
  46. WriteLn(P.Birthday.Year);              (* WriteLn(P.FBirthday.Year);     *)
  47. WriteLn('I am ',P.Age,' years old!');  (* WriteLn(... P.GetAge ...);     *)
  48.  
  49. So the question is: how, using macros or templates or whatever, would you
  50. gain the elegance and simplicity of this feature using Standard C++?  Note
  51. that I'm not debating whether you _need_ it or not; obviously we survived
  52. for years without it and could go on a lot longer quite happily, but as I
  53. said, I'm curious.
  54.  
  55. Here, for the benfit of philist^H^H^H^Hnon-Pascal-programmers, is the type
  56. definition in what I think is standard C -- except for the property parts,
  57. which I'll fudge:
  58.  
  59. struct TDate
  60. { int Year, Month, Day; };
  61.  
  62. class TPerson
  63. {
  64.   private:
  65.     TDate FBirthday;
  66.     void SetBirthday(TDate ABirthday);
  67.     int GetAge();
  68.   public:
  69.     property TDate Birthday  read FBirthday  write SetBirthday;  // ???
  70.     property int Age  read GetAge;  // ???
  71. }; 
  72. -- 
  73. Let's just say that if complete and utter chaos was light- |    Paul Sleigh
  74. ning, he'd be the sort to stand on a hilltop in a thunder- | Eric the Fruitbat
  75. storm wearing wet copper armour and shouting 'All gods are | fruitbat@canberra
  76. bastards'.   [T Pratchett: Rincewind discussing Twoflower] |   .DIALix.oz.au
  77.